package com.snowj.apaas.service.impl;

import com.snowj.apaas.domain.auto.AgreementChangeLog;
import com.snowj.apaas.repository.auto.AgreementChangeLogMapper;
import com.snowj.apaas.service.AgreementChangeLogService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AgreementChangeLogServiceImpl implements AgreementChangeLogService {

    private final AgreementChangeLogMapper agreementChangeLogMapper;

    public AgreementChangeLogServiceImpl(AgreementChangeLogMapper agreementChangeLogMapper) {
        this.agreementChangeLogMapper = agreementChangeLogMapper;
    }

    /**
     * 列出合同变更日志
     *
     * @param pageable
     * @return
     */
    @Override
    public Page<AgreementChangeLog> listAgreementChangeLogs(Pageable pageable) {
        List<AgreementChangeLog> agreementChangeLogs = agreementChangeLogMapper.select(c -> {
            return c.limit(pageable.getPageSize()).offset((long) pageable.getPageSize() * pageable.getPageNumber());
        });
        long total = agreementChangeLogMapper.count(c -> c);
        return new PageImpl<>(agreementChangeLogs, pageable, total);
    }

    /**
     * 创建合同变更日志
     *
     * @param agreementChangeLog
     * @return
     */
    @Override
    public AgreementChangeLog createAgreementChangeLog(AgreementChangeLog agreementChangeLog) {
        agreementChangeLogMapper.insert(agreementChangeLog);
        return agreementChangeLog;
    }
}
